package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.message.MessageConstant;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.querypage.QueryPageBean;
import com.itheima.results.PageResult;
import com.itheima.results.Result;
import com.itheima.service.CheckGroupService;
import com.itheima.utils.QiniuUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    //分页查询
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return checkGroupService.findPage(queryPageBean);
    }

        //查询所有检查组
        @RequestMapping("/findAll.do")
        public Result findAllCheckItem() {
            try {
                List<CheckGroup> list = checkGroupService.findAllCheckItem();
                return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
            }
        }
    //添加检查组
    @RequestMapping("/add.do")
    public Result addCheckGroup(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            checkGroupService.addCheckGroup(checkGroup, checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);//新增失败
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);//新增成功
    }

    //根据id查询所有检查组信息
    @RequestMapping("/findById.do")
    public Result findById(Integer checkgroupId) {
        try {
            CheckGroup checkGroup = checkGroupService.findById(checkgroupId);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }
    //根据检查组id 查询到中间表关联的检查项
    @RequestMapping("/findCheckitemIdsByCheckgroupId.do")
    public Result findCheckitemIdsByCheckgroupId(Integer checkgroupId) {
        try {
            List<Integer> list = checkGroupService.findCheckitemIdsByCheckgroupId(checkgroupId);
            return  new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
    //修改编辑表单数据
    @RequestMapping("/edit.do")
    public Result edit(@RequestBody CheckGroup checkGroup ,Integer[] checkitemIds){
        try {
            checkGroupService.edit(checkGroup,checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }


}
